<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use DB;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {            
            $this->couponStop();
            $this->groupbuyStop();
        })->everyFiveMinutes();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

    /**
     * 优惠券作废
     */
    private function couponStop()
    {
        $time = date('Y-m-d H:i:s');
        DB::table('user_coupons')->where('end_date','<',$time)->delete();
    }

    /**
     * 拼团过期
     */
    private function groupbuyStop()
    {
        $time = date('Y-m-d H:i:s');
        DB::table('user_groupbuy')
            ->where('end_at','<',$time)
            ->where('parent_id',0)
            ->update(['status'=>'-1']);
    }
}
